//
//  HWGameTestController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/21.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWGameTestController: HWBaseViewController {
    var topImageView:UIImageView!
    var httpManager = HWGameTestHttpManager()
    var tableView:UITableView!
    var tableViewProtocol = HWGameTestProtocol()
    var modelArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addRefresh()
        
        loadData()
        
        addAction()
    }

}

extension HWGameTestController {
    private func initUI() -> Void {
        navigationView.isHidden = false
        navigationView.backButton.setImage(UIImage(named: "whiteNavBack"), for: .normal)
        navigationView.titleLabel.textColor = .white
        navigationView.titleLabel.text = "任务赚金"
        
        UIColor.gradientColor(view: view, colors: [UIColor.colorWithHexString(hexString: "#FA8C08").cgColor,UIColor.colorWithHexString(hexString: "#FA8C08").cgColor,UIColor.colorWithAlphaHexString(hexString: "#FF9D28", alpha: 0).cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 1))
        
        topImageView = UIImageView()
        topImageView.image = UIImage(named: "taskTakeTop")
        view.addSubview(topImageView)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
    }
    
    private func initFrame() -> Void {
        topImageView.snp.makeConstraints { make in
            make.left.equalTo(30*widthScale)
            make.top.equalTo(navigationView.snp.bottom).offset(13*widthScale)
            make.width.equalTo(316*widthScale)
            make.height.equalTo(69*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topImageView.snp.bottom)
        }
    }
}

extension HWGameTestController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWGameTestController {
    func loadData() -> Void {
        tableView.mj_footer?.isHidden = true
        httpManager.getGameTestList(keyword:"") { models in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.tableView.mj_footer?.isHidden = false
                self.modelArr.removeAllObjects()
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameTestModel]
            self.tableView.reloadData()
        } failure: { errorString in
            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
    }
    
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
        tableView.mj_footer?.isHidden = true
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        httpManager.getGameTestListFooter(keyword: "") { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameTestModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
    }
    
}

extension HWGameTestController {
    func addAction() -> Void {
        self.tableViewProtocol.rightButtonClick = {
            model in
            if HWLocallyStoredData.getLoginUserID().count > 0 {
                if model.status == "0" {
                    self.httpManager.getGameTestTask(id: model.id) { success in
                        self.loadData()
                        self.view.showHUD(title: success)
                    } failure: { errorString in
                        self.view.showHUD(title: errorString)
                    }

                }else {
                    self.httpManager.getGameTestReceive(id: model.id) { success in
                        self.loadData()
                    } failure: { errorString in
                        self.view.showHUD(title: errorString)
                    }
                }
            } else {
                self.navigationController?.pushViewController(HWLoginController(), animated: true)
            }
            
        }
        
        self.tableViewProtocol.didClickItem = {
            model in
            let alert = HWGameTestAlert()
            alert.model = model
            alert.show()
        }
    }
}
